package com.top.horizon.th1804app.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.math.BigDecimal;
import java.util.ArrayList;

public class PieView extends View {
    // 颜色表
    private int[] mColors = {0xFFBDBDBD, 0xFF29CC06, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080,
            0xFFE6B800, 0xFF7CFC00};
    // 饼状图初始绘制角度
    private float mStartAngle = 0;
    // 数据
    private ArrayList<PieData> mData;
    // 宽高
    private int mWidth, mHeight;
    // 画笔
    private Paint mPaint;
    private Paint mTextPaint;

    public PieView(Context context) {
        this(context, null);
    }

    public PieView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextSize(18);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null == mData)
            return;
        float currentStartAngle = mStartAngle;                    // 当前起始角度
        canvas.translate(mWidth / 2, mHeight / 2);                // 将画布坐标原点移动到中心位置
        float r = (float) (Math.min(mWidth, mHeight) / 2);  // 饼状图半径
        RectF rect = new RectF(-r, -r, r, r);                     // 饼状图绘制区域

        for (int i = 0; i < mData.size(); i++) {
            PieData pie = mData.get(i);
            mPaint.setColor(pie.getColor());
            canvas.drawArc(rect, currentStartAngle, pie.getAngle(), true, mPaint);
            Log.e("起始角度", "第" + i + "个" + currentStartAngle + "==左边开始，右边结束==" + pie.getAngle());

            float pxt = (float) ((r / 1.2) * Math.cos(Math.toRadians(currentStartAngle + pie.getAngle() / 2)));
            float pyt = (float) ((r / 1.2) * Math.sin(Math.toRadians(currentStartAngle + pie.getAngle() / 2)));
            currentStartAngle += pie.getAngle();
            int resToRound = (int) pie.getValue();
            //提供精确的小数位四舍五入处理。
//            double resToRound = round(res, 2);
            Log.e("当前扇形所占百分比", "第" + i + "个" + pie.getPercentage() + "====");
            if (pie.getPercentage() > 0.02) {
                if (currentStartAngle % 360.0 >= 90.0 && currentStartAngle % 360.0 <= 270.0) {//2 3 象限
                    canvas.drawText(resToRound + "G", pxt + mTextPaint.measureText(resToRound + "G"), pyt , mTextPaint);
                    Log.e("第" + i + "个" +"起始位置", pxt + "=="+ pie.getAngle()+"==" + pyt);
                } else {
                    if (  pie.getAngle() > 90){
                        canvas.drawText(resToRound + "G", pxt + mTextPaint.measureText(resToRound + "G"), pyt + 4, mTextPaint);
                    }else {
                        Log.e("第" + i + "个" + "起始", pxt + "==" + pie.getAngle() + "==" + pyt);
                        canvas.drawText(resToRound + "G", pxt - mTextPaint.measureText(resToRound + "G"), pyt + 4, mTextPaint);
                    }
                }
            }


        }

    }

    /**
     * 提供精确的小数位四舍五入处理。
     *
     * @param v     需要四舍五入的数字
     * @param scale 小数点后保留几位
     * @return 四舍五入后的结果
     */
    public static double round(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal ne = new BigDecimal("1");
        return b.divide(ne, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }



    // 设置数据
    public void setData(ArrayList<PieData> mData) {
        this.mData = mData;
        initDate(mData);
        invalidate();   // 刷新
    }

    // 初始化数据
    private void initDate(ArrayList<PieData> mData) {
        if (null == mData || mData.size() == 0)   // 数据有问题 直接返回
            return;

        float sumValue = 0;
        for (int i = 0; i < mData.size(); i++) {
            PieData pie = mData.get(i);

            sumValue += pie.getValue();       //计算数值和

            int j = i % mColors.length;       //设置颜色
            pie.setColor(mColors[j]);
        }

        float sumAngle = 0;
        for (int i = 0; i < mData.size(); i++) {
            PieData pie = mData.get(i);

            float percentage = pie.getValue() / sumValue;   // 百分比
            float angle = percentage * 360;                 // 对应的角度

            pie.setPercentage(percentage);                  // 记录百分比
            pie.setAngle(angle);                            // 记录角度大小
            sumAngle += angle;

            Log.i("angle", "" + pie.getAngle());
        }
    }
}